<template>
  <div class="hospitalsign">
    <div class="top">
      <div class="title">出国境申请信息</div>
      <div class="topinfo">
        <div class="post">前往国家或地区</div>
        <div><input
            type="text"
            placeholder="请输入国家或地区名称"
            v-model="form.countryName"
          /></div>
      </div>
      <div class="topinfo has-arrow">
        <div class="post">类别</div>
        <div
          @click="showPicker('reason')"
          :class="{ unfill: !form.reason.value }"
        >
          {{ form.reason.label || "未填写" }}
        </div>
      </div>
      <div class="topinfo">
        <div class="post">是否涉密</div>
        <div>
          <div>
            <van-radio-group v-model="form.secretLevel" direction="horizontal">
              <van-radio v-for="(item,i) of sfsmry" :name="item.value" :key="i">{{item.label}}</van-radio>
            </van-radio-group>
          </div>
        </div>
      </div>
      <div class="topinfo has-arrow" @click="showDatePicker('startDate')">
        <div class="post">计划行程开始时间</div>
        <div :class="{ unfill: !form.startDate }">
          {{ form.startDate | dateFormat("请选择") }}
        </div>
      </div>
      <div class="topinfo has-arrow" @click="showDatePicker('endDate')">
        <div class="post">计划行程结束时间</div>
        <div :class="{ unfill: !form.endDate }">
          {{ form.endDate | dateFormat("请选择") }}
        </div>
      </div>
       <div class="topinfo">
        <div class="post">天数</div>
        <div>{{days}}天</div>
      </div>
      <div class="topinfo has-arrow">
        <div class="post">旅游类型</div>
        <div
          @click="showPicker('travelType')"
          :class="{ unfill: !form.travelType.value }"
        >
          {{ form.travelType.label || "未填写" }}
        </div>
      </div>
      <div class="topinfo">
        <div class="post">途径国家或地区</div>
        <div><input
            type="text"
            placeholder="请输入国家或地区名称"
            v-model="form.passCountryName"
          /></div>
      </div>
      <div class="topinfo">
        <div class="post">拟探望人姓名</div>
        <div><input
            type="text"
            placeholder="请输入拟探望人姓名"
            v-model="form.personName"
          /></div>
      </div>
      <div class="topinfo">
        <div class="post">拟探望人年龄</div>
        <div><input
            type="number"
            placeholder="请输入年龄"
            v-model="form.personAge"
          /></div>
      </div>
      <div class="topinfo">
        <div class="post">拟探望人关系</div>
        <div><input
            type="text"
            placeholder="请输入关系"
            v-model="form.personRelationship"
          /></div>
      </div>
      <div class="topinfo">
        <div class="post">拟探望人住址</div>
        <div><input
            type="text"
            placeholder="请输入住址"
            v-model="form.personAddress"
          /></div>
      </div>
      <div class="topinfo">
        <div class="post">拟探望人工作单位</div>
        <div><input
            type="text"
            placeholder="请输入工作单位"
            v-model="form.personOrganization"
          /></div>
      </div>
      <div class="topinfo">
        <div class="post">家庭联系人姓名</div>
        <div><input
            type="text"
            placeholder="请输入姓名"
            v-model="form.homeContactName"
          /></div>
      </div>
      <div class="topinfo">
        <div class="post">家庭联系人关系</div>
        <div><input
            type="text"
            placeholder="请输入关系"
            v-model="form.homeContactRelationship"
          /></div>
      </div>
      <div class="topinfo">
        <div class="post">家庭联系人电话</div>
        <div><input
            type="text"
            placeholder="请输入电话"
            v-model="form.homeContactMobile"
          /></div>
      </div>
      <div class="topinfo" style="border-bottom: none;">
        <div class="post">文件</div>
        <div>
          <van-uploader :preview-image="false" v-model="fileList" :after-read="afterRead">
            <van-button icon="plus" type="primary"><span class="ploadSies">上传文件</span></van-button>
          </van-uploader>
        </div>
      </div>
      <div class="topinfo noborder" v-show="fileList.length">
        <div v-for="(val,index) in fileList" :key="index">
          <span class="ploadSies">{{val.file.name}}</span><van-icon name="cross" @click="removeImage(val)" />
        </div>
        <div></div>
      </div>
    </div>
    <div class="sign-btn" v-if="AbroadApply == '1'" @click="showVanDialog = true">提交申请</div>
    <div class="sign-btn" v-else @click="promiseBook">提交申请</div>
    <van-dialog v-model="showVanDialog" :width="'90vw'" :show-cancel-button="false" :show-confirm-button="false">
      <div class="showVanDialog">
        <div class="showVanDialog_cross" @click="showVanDialog = false">
          <van-icon name="cross" />
        </div>
        <div class="showVanDialog_tit">
          <h3>全国人大机关因私出国(境)人员承诺书</h3>
          <p>因私出国(境)期间,本人郑重承诺</p>
        </div>
        <div class="showVanDialog_content" style="height: 50vh">
          <div>
            <p>一、严格遵守《中华人民共和国国家安全法》、《中华人民共和国反间谍法》、《中华人民共和国保守国家秘密法》等相关法律法规</p>
            <p>二、严格遵守政治规矩和纪律,加强请示报告</p>
            <p>三、严格遵守保密制度,严密保守所知悉的国家秘密</p>
            <p>四、严格遵守外事纪律,不出入违法复杂场所</p>
            <p>五、坚定维护国家利益和形象,不发表违背国家政策的言论</p>
            <p>六、不参加民运、法轮功、“疆独”、“藏独”等敌对势力组织的活动</p>
            <p>七、费用自理,不接受境外机构、人员的资助</p>
            <p>八、不接受境外媒体的采访</p>
            <p>九、回国后如实向本单位报告出国(境)期间的情况,做到不谎报、瞒报和漏报。</p>
          </div>
          <div class="showVanDialog_button">
            <van-button round type="info" :color="'#ccc'" @click="showVanDialog = false">不同意</van-button>
            <van-button round type="info" @click="submit">同意</van-button>
          </div>
        </div>
      </div>
    </van-dialog>
    <div class="historylist">
      <p class="historytitle">登记记录</p>
      <div class="item" v-for="(item, i) in list" :key="i">
        <div>
          <p>申请日期：{{ item.createTime }}</p>
          <p>{{ item.qwgjhdq }}</p>
        </div>
        <div @click="ReturnVisitForm(item)">{{item.isBm === 0 ? '填写回访表' : '已填回访表'}}</div>
      </div>
    </div>
    <van-dialog :width="'90vw'" v-model="showCxly" @confirm="showCxlyconfirm" title="出行的理由" show-cancel-button>
      <van-field
        v-model="message"
        rows="6"
        autosize
        class="showCxlyMessage"
        type="textarea"
        placeholder="请输入出行的理由"
        show-word-limit
      />
    </van-dialog>
    <picker
      title="请选择"
      :columns="pickerOptions.pickerColumns"
      @fetchData="setData"
      :show="show"
      @cancel="hidePicker"
      @hide="hidePicker"
    ></picker>
    <date-picker
      @fetchData="fetchDate"
      date-type="date"
      :default-date="form[dateType]"
      :show="isDatePickerShow"
      @cancel="hideDatePicker"
      @hide="hideDatePicker"
    ></date-picker>
  </div>
</template>

<script>
import picker from '../components/picker';
import datePicker from '../components/datePicker';
import moment from 'moment';
import { Dialog } from 'vant';
import { getsy, getlylx, getsfsmry, yscgjQuerydata4list, yscgjQuerydata4object, yscgjSavedata, yscgjJy, yscgjSavedata3cxdly, uploadoneattachment } from '@/assets/js/api';
const selections = {
  relationship: [],
  travelType: [],
  reason: []
};
export default {
  components: {
    picker,
    datePicker
  },
  filters: {
    dateFormat(v, defaultValue) {
      return (v && moment(v).format('YYYY-MM-DD')) || defaultValue;
    }
  },
  data() {
    return {
      message: '',
      djxxId: '',
      showCxly: false,
      showVanDialog: false,
      dspToken: localStorage.dspToken,
      type: '',
      show: false,
      pickerOptions: {
        pickerColumns: []
      },
      form: {
        countryName: '',
        reason: {},
        travelType: {},
        secretLevel: '',
        personName: '',
        personAge: '',
        personRelationship: '',
        personAddress: '',
        personOrganization: '',
        homeContactName: '',
        homeContactRelationship: '',
        homeContactMobile: '',
        // homeMembersInfo: '',
        passCountryName: '',
        startDate: undefined,
        endDate: undefined,
        images: []
      },
      dateType: '',
      isDatePickerShow: false,
      sfsmry: [],
      fileList: [],
      list: [],
      AbroadApply: String(sessionStorage.AbroadApply)
    };
  },
  computed: {
    days() {
      return Number(moment(this.form.endDate).diff(moment(this.form.startDate), 'days')) + 1;
    }
  },
  mounted() {
    this.loadOption(getlylx, 'travelType');
    this.loadOption(getsy, 'reason');
    getsfsmry().then(({data}) => {
      this.sfsmry = data.map(o => ({
        label: o.name,
        value: o.val
      }));
    });
    this.getyscgjQuerydata4list();
    yscgjQuerydata4object({
      dspToken: this.dspToken
    }).then(({data}) => {});
    if (this.AbroadApply === '1') {
      this.form = {
        ...this.form,
        ...JSON.parse(sessionStorage.yscgjSavedataJson)
      };
    }
  },
  methods: {
    ReturnVisitForm(item) {
      if (item.isBm !== 0) { return; }
      Dialog.confirm({
        message: '是否已出境'
      })
        .then(() => {
          this.$router.push({
            name: 'ReturnVisitForm',
            query: {
              title: '填写回访表',
              method: 'questions',
              id: item.djxxId
            }
          });
        })
        .catch(() => {
          this.djxxId = item.djxxId;
          this.showCxly = true;
        });
    },
    showCxlyconfirm() {
      yscgjSavedata3cxdly({
        dspToken: this.dspToken,
        cxdly: this.message,
        djxxId: this.djxxId
      }).then(({data}) => {
        this.$toast('填写回访表成功！');
        this.djxxId = '';
        this.message = '';
      });
    },
    getyscgjQuerydata4list() {
      yscgjQuerydata4list({
        dspToken: this.dspToken
      }).then(({data}) => {
        this.list = data;
      });
    },
    promiseBook() {
      let FIELD_MAP = {
        jhkssj: '计划行程开始时间',
        jhjssj: '计划行程结束时间',
        qwgjhdq: '前往国家或地区',
        sfsmry: '是否涉密',
        sy: '类别(事由)'
      };
      if (this.form.reason.value === 1) {
        FIELD_MAP.lylx = '旅游类型';
      } else if (this.form.reason.value === 2) {
        FIELD_MAP.ntwr = '拟探望人';
        FIELD_MAP.ntwrdz = '拟探望人地址';
        FIELD_MAP.ntwrgx = '拟探望人关系';
        FIELD_MAP.ntwrgzdw = '拟探望人工作单位';
        FIELD_MAP.ntwrnl = '拟探望人年龄';
      }
      const params = {
          jhkssj: moment(this.form.startDate).format('YYYY-MM-DD'), // 计划行程开始时间
          jhjssj: moment(this.form.endDate).format('YYYY-MM-DD'), // 计划行程结束时间
          jtlxrdh: this.form.homeContactMobile, // 家庭联系人联系电话
          jtlxrgx: this.form.homeContactRelationship, // 家庭联系人关系
          jtlxrxm: this.form.homeContactName, // 家庭联系人姓名
          lylx: this.form.travelType.value, // 旅游类型
          ntwr: this.form.personName, // 拟探望人
          ntwrdz: this.form.personAddress, // 拟探望人地址
          ntwrgx: this.form.personRelationship, // 拟探望人关系
          ntwrgzdw: this.form.personOrganization, // 拟探望人工作单位
          ntwrnl: this.form.personAge, // 拟探望人年龄
          qwgjhdq: this.form.countryName, // 前往国家或地区
          sy: this.form.reason.value, // 类别(事由)
          sfsmry: this.form.secretLevel // 是否涉密
        }, key = Object.keys(FIELD_MAP).find(key => !params[key]);
      if (key) {
        this.$toast(`请填写【${FIELD_MAP[key]}】项内容`);
      } else {
        sessionStorage.yscgjSavedataJson = JSON.stringify(this.form);
        yscgjJy({
          qwgjhdq: params.qwgjhdq,
          jtlxrdh: params.jhjssj,
          dspToken: this.dspToken
        }).then(({pkValue}) => {
          if (pkValue === '1') {
            this.showVanDialog = true;
          } else {
            this.$router.push({
              name: 'AbroadEntryExitApplication',
              query: { id: pkValue }
            });
          }
        });
      }
    },
    showPicker(type) {
      this.type = type;
      this.show = true;
      this.pickerOptions.pickerColumns = selections[type];
    },
    hidePicker(d) {
      this.show = d.showPicker;
    },
    setData(d) {
      this.form[this.type] = d.value;
      this.show = d.showPicker;
    },
    showDatePicker(type) {
      this.isDatePickerShow = true;
      this.dateType = type;
    },
    fetchDate(d) {
      this.form[this.dateType] = d.originValue;
      this.isDatePickerShow = d.showDatePicker;
    },
    hideDatePicker(d) {
      this.isDatePickerShow = d.showDatePicker;
    },
    removeImage(val) {
      this.fileList = this.fileList.filter((item, index) => {
        if (val.content === item.content) {
          this.form.images.splice(index, 1);
        } else {
          return item;
        }
      });
    },
    afterRead(file) {},
    async uploadoneattachment(pkValue) {
      for (let item of this.fileList) {
        let formData = new FormData();
        formData.append('file', item.file);
        formData.append('busiId', pkValue || 'jggb');
        formData.append('dspToken', this.dspToken);
        formData.append('busiTable', 'T_LGB_YSCGJ_DJXX');
        formData.append('busiType', 'jggb_yscgjtp');
        await uploadoneattachment(formData).then(res => {
          console.log(res);
        });
      }
      this.fileList = [];
      this.showVanDialog = false;
      this.$toast('提交成功');
      sessionStorage.yscgjSavedataJson = '{}';
      sessionStorage.AbroadApply = '0';
      this.getyscgjQuerydata4list();
      this.form = {
        countryName: '',
        reason: {},
        travelType: {},
        secretLevel: '',
        personName: '',
        personAge: '',
        personRelationship: '',
        personAddress: '',
        personOrganization: '',
        homeContactName: '',
        homeContactRelationship: '',
        homeContactMobile: '',
        // homeMembersInfo: '',
        passCountryName: '',
        startDate: undefined,
        endDate: undefined,
        images: []
      };
    },
    loadOption(api, fieldName) {
      return api().then(({data}) => {
        selections[fieldName] = data.map(o => ({
          label: o.name,
          value: o.val
        }));
      });
    },
    submit() {
      const params = {
        jhkssj: moment(this.form.startDate).format('YYYY-MM-DD'), // 计划行程开始时间
        jhjssj: moment(this.form.endDate).format('YYYY-MM-DD'), // 计划行程结束时间
        jtlxrdh: this.form.homeContactMobile, // 家庭联系人联系电话
        jtlxrgx: this.form.homeContactRelationship, // 家庭联系人关系
        jtlxrxm: this.form.homeContactName, // 家庭联系人姓名
        lylx: this.form.travelType.value, // 旅游类型
        ntwr: this.form.personName, // 拟探望人
        ntwrdz: this.form.personAddress, // 拟探望人地址
        ntwrgx: this.form.personRelationship, // 拟探望人关系
        ntwrgzdw: this.form.personOrganization, // 拟探望人工作单位
        ntwrnl: this.form.personAge, // 拟探望人年龄
        qwgjhdq: this.form.countryName, // 前往国家或地区
        sy: this.form.reason.value, // 类别(事由)
        sfsmry: this.form.secretLevel, // 是否涉密
        images: this.form.images.join(','), // 图片数组
        djxxId: this.form.djxxId
      };
      yscgjSavedata({
        ...params,
        dspToken: this.dspToken
      }).then(({pkValue}) => {
        this.uploadoneattachment(pkValue);
      });
    }
  }
};
</script>

<style lang="less" scoped>
.showCxlyMessage {
  font-size: 30px;
  line-height: 38px;
}
.showVanDialog {
  padding: 15px;
  .showVanDialog_tit {
    font-size: 30px;
    padding: 0 20px;
  }
  .showVanDialog_cross {
    text-align: right;
  }
  .showVanDialog_content {
    overflow-y: auto;
    padding: 0 20px 20px 20px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .showVanDialog_button {
      margin-top: 30px;
      display: flex;
      justify-content: space-around;
      .van-button {
        width: 110px;
        font-size: 32px;
      }
      .van-button--normal {
        padding: 12px 30px;
        box-sizing: content-box;
      }
    }
  }
}
.hospitalsign {
  padding: 24px;
  background: #f5f5f5;
  .top {
    width: 704px;
    background-color: #ffffff;
    box-shadow: 3px 4px 9px 1px rgba(1, 88, 155, 0.05);
    border-radius: 8px;
    margin: 0 auto;
    .title {
      height: 64px;
      background-color: #3198ed;
      color: #ffffff;
      font-size: 30px;
      line-height: 64px;
      padding: 0 16px;
      border-top-left-radius: 8px;
      border-top-right-radius: 8px;
    }
    .topinfo {
      display: flex;
      position: relative;
      padding: 35px 18px;
      box-sizing: border-box;
      width: 674px;
      margin: 0 auto;
      font-size: 34px;
      color: #343434;
      justify-content: space-between;
      &:not(:last-child, .noborder) {
        border-bottom: 1px solid #eeeeee;
      }
      .ploadSies {
        font-size: 25px;
      }
      &.has-arrow {
        position: relative;
        padding-right: 32px;
        &::before {
          content: "\F00A";
          position: absolute;
          right: 0;
          top: 50%;
          transform: translateY(-50%);
          font: normal normal normal 1.867vw/1 vant-icon;
          color: #b2b2b2;
          font-size: inherit;
          text-rendering: auto;
          -webkit-font-smoothing: antialiased;
        }
        > .unfill {
          color: #bfbfbf;
        }
      }
      .post {
        color: #888888;
        text-align: left;
        min-width: 200px;
        // white-space: nowrap;
      }
      div:last-child {
        // flex: 1;
        text-align: right;
      }
      .special {
        width: 380px;
      }
      input {
          text-align: right;
      }
      .arrow {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        font-size: 34px;
        color: #888;
        text-align: right;
      }
      /deep/ .van-radio-group {
        line-height: 1.5;
        .van-radio {
          &:not(:last-child) {
            padding-right: 20px;
          }
          .van-radio__icon {
            font-size: inherit;
          }
          .van-radio__label {
            height: auto;
            line-height: inherit;
          }
        }
      }
      /deep/ .van-uploader__wrapper {
        .van-uploader__upload {
          width: 156px;
          height: 156px;
          background-color: #bfbfbf;
          .van-icon {
            font-size: 40px;
            color: #fff;
            &:before {
              content: "\F0A2";
            }
          }
        }
        .van-uploader__mask {
          .van-icon {
            font-size: 40px;
            color: #fff;
          }
        }
        .van-uploader__preview-image {
          width: 156px;
          height: 156px;
          .van-icon {
            font-size: 40px;
            color: #fff;
            &:before {
              content: "\F0A2";
            }
          }
        }
        .van-uploader__preview-delete {
          width: 30px;
          height: 30px;
          border-radius: 15px;
          .van-uploader__preview-delete-icon {
            position: absolute;
            font-size: 12px;
            right: 2px;
            top: 2px;
            transform: scale(0.6);
          }
        }
      }
    }
    .noborder {
        padding-top: 0;
        font-size: 20px;
        display: block;
        &>div {
          width: 100%;
          white-space: nowrap;
          text-overflow: ellipsis;
          overflow: hidden;
          word-break: break-all;
          display: flex;
          align-items: center;
          justify-content: space-between;
        }
      }
  }
  .sign-btn {
    border: none;
    width: 704px;
    height: 90px;
    background-color: #3198ed;
    box-shadow: 0px 5px 5px 0px rgba(89, 166, 250, 0.1);
    border-radius: 45px;
    line-height: 90px;
    color: #ffffff;
    font-size: 36px;
    text-align: center;
    margin: 26px auto;
    letter-spacing: 7px;
  }
  .historylist {
    .historytitle {
      color: #888888;
      font-size: 30px;
      margin: 19px 0;
    }
    .item {
      width: 704px;
      // height: 140px;
      background-color: #ffffff;
      box-shadow: 3px 4px 9px 1px rgba(1, 88, 155, 0.05);
      border-radius: 8px;
      margin: 0 auto;
      margin-top: 23px;
      position: relative;
      padding: 31px 30px 26px 33px;
      box-sizing: border-box;
      display: flex;
      &::before {
        content: "";
        width: 5px;
        // height: 140px;
        background-color: #3198ed;
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        border-top-left-radius: 50px;
        border-bottom-left-radius: 50px;
      }
      > div:first-child {
        flex: 1;
        padding-right: 10px;
        p:first-child {
          color: #888888;
          font-size: 30px;
        }
        p:last-child {
          color: #353535;
          font-size: 34px;
          // white-space: nowrap;
          // text-overflow: ellipsis;
          // overflow: hidden;
        }
      }
      > div:last-child {
        font-size: 24px;
        color: #b2b2b2;
        align-self: center;
        text-align: right;
        background-color: #d0d0d0;
        border-radius: 29px;
        padding: 6px 20px;
        color: #353535;
      }
    }
  }
}
.van-button--primary {
  background-color: #ccc;
  border: 1px solid #ccc;
  border-radius: 50px;
}
</style>
